<?php
namespace app\api\controller;

use think\Controller;
use think\Request;

class Invoices extends Controller
{
    public function getInvoices(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $invoices = new \app\admin\model\Invoices();

        $where['user_id'] = $data['user_id'];
        $rs = $invoices->getOne($where);
        if($rs == null){
            //查询是否有此用户
            $user = new \app\admin\model\User();
            $userWhere['id'] = $data['user_id'];
            $isUser = $user->getOne($userWhere);
            if($isUser){
                //发票增加信息
                $addData['user_id'] = $where['user_id'];
                $addData['com_name'] = '';
                $addData['tax_num'] = '';
                $addData['address'] = '';
                $addData['tel'] = '';
                $addData['bank'] = '';
                $addData['bank_num'] = '';
                $invoices->add($addData);
                $res['data']['code'] = 1;
                $res['data']['msg'] = '获取成功';
                $res['data']['list'] = $addData;
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = '获取失败';
            }
        }else{
            $res['data']['code'] = 1;
            $res['data']['msg'] = '获取成功';
            $res['data']['list'] = $rs;
        }

        $res['status'] = 200;
        echo json_encode($res);
    }

    public function editInvoices(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $invoices = new \app\admin\model\Invoices();

        $where['user_id'] = $data['user_id'];
        $rs = $invoices->edit($where,$data);
        if($rs == null){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '修改失败';
        }else{
            $res['data']['code'] = 1;
            $res['data']['msg'] = '修改成功';
        }

        $res['status'] = 200;
        echo json_encode($res);
    }

}